package cn.change.basic.service.impl;

import cn.change.basic.mapper.BaseMapper;
import cn.change.basic.query.BaseQuery;
import cn.change.basic.service.lBaseService;
import cn.change.basic.util.PageList;
import cn.change.org.domain.Shop;
import cn.change.org.query.ShopQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 16252
 * @Transactional事物的传播机制：保证一组操作只有一个操作
 * 部门服务实现类
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public abstract class BaseServiceImpl<T> implements lBaseService<T> {
    @Autowired
    BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T queryById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.loadAll();
    }


    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询总条数
        Long totals = baseMapper.queryCount(query);
        if (totals == 0){
            return new PageList<>();
        }
        //查询当前页面的数据
        List<T> rows = baseMapper.queryDate(query);
        return new PageList<T>(totals, rows);
    }
    /**
     * 批量删除
     */
    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }
}
